package icom.acm.manager;

import icom.acm.bean.T_USERONLINE;
import icom.acm.util.DBConnect;

import java.util.HashMap;
import java.util.Map;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

public class T_UserOnlineMgr {
	public T_USERONLINE selectById(DBConnect db, Integer id){
		T_USERONLINE e = new T_USERONLINE();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("UO_U_ID", id);
		String sql = e.toSqlSelectEqual(params);
		db.prepareStatement(sql);
		db.excuteQuery();
		db.next();
		e.set(db);
		return e;
	}
	
	public void insert(T_USERONLINE uo){
		String sql = "INSERT INTO T_USERONLINE(UO_U_ID, UO_LOGINTIME, UO_SESSIONID)" +
				" VALUES("+uo.getUO_U_ID()+", NOW(), " + uo.getUO_SESSIONID() + ")";
		DBConnect db = new DBConnect();
		db.prepareStatement(sql);
		try {
			db.excute();
		} catch (MySQLIntegrityConstraintViolationException e) {
			e.printStackTrace();
		} finally {	
			db.close();
		}
	}
	/**
	 * 插入用户在线
	 * @param uo
	 * @param db
	 * @throws MySQLIntegrityConstraintViolationException 
	 */
	public void insert(T_USERONLINE uo, DBConnect db) throws MySQLIntegrityConstraintViolationException{
		String sql = "INSERT INTO T_USERONLINE(UO_U_ID, UO_LOGINTIME, UO_SESSIONID)" +
			" VALUES("+uo.getUO_U_ID()+", NOW(), '" + uo.getUO_SESSIONID() + "')";
		db.prepareStatement(sql);
		db.excute();
	}
	
	public void deleteById(Integer id){
		String sql = "DELETE FROM T_USERONLINE WHERE UO_U_ID="+id;
		DBConnect db = new DBConnect();
		
		db.prepareStatement(sql);
		try {
			db.excute();
		} catch (MySQLIntegrityConstraintViolationException e) {
			e.printStackTrace();
		} finally {
			db.close();
		}
	}
	
	public void deleteAll(){
		String sql = "DELETE FROM T_USERONLINE WHERE UO_U_ID>=0";
		DBConnect db = new DBConnect();
		
		db.prepareStatement(sql);
		try {
			db.excute();
		} catch (MySQLIntegrityConstraintViolationException e) {
			e.printStackTrace();
		} finally {
			db.close();
		}
	}

	public T_USERONLINE selectBySessionId(DBConnect db, String sessionid) {
		T_USERONLINE e = new T_USERONLINE();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("UO_SESSIONID", sessionid);
		String sql = e.toSqlSelectEqual(params);
		db.prepareStatement(sql);
		db.excuteQuery();
		db.next();
		e.set(db);
		return e;
	}
}
